<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>出车记录</title>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css">
<style type="text/css">
th, td, tr, p {
	text-align: center;
}
</style>
</head>
<body>
	<!-- 面板 -->
	<div class="panel panel-default">
		<!-- 面板头-->
		<div class="panel-heading">
			<i class="glyphicon glyphicon-th"> 出车记录表</i>
		</div>
		<!-- 面板体-->
		<div class="panel-body">
			<form class="form-inline" role="form" style="float: left;" id="query_form">
				<div class="dropdown">
					<button type="button" class="btn dropdown-toggle"
						id="dropdownMenu1" data-toggle="dropdown" >
						车牌号码 <span class="caret"></span>
					</button>
					<input class="form-control has-success" type="text" name="doutCarlicense"
						placeholder="此处输入车牌号码，支持模糊查询" id="hold">
					<button type="button" class="btn btn-warning" id="query">
						<i class="glyphicon glyphicon-search"></i> 查询
					</button>
					<ul class="dropdown-menu" role="menu"
						aria-labelledby="dropdownMenu1">
						<li role="presentation"><a role="menuitem" tabindex="-1"
							href="#" id="doutCarlicense">车牌号码</a></li>
						<li role="presentation"><a role="menuitem" tabindex="-1"
							href="#" id="doutDept">部门</a></li>
						<li role="presentation"><a role="menuitem" tabindex="-1"
							href="#" id="doutFollower">随行人员</a></li>
						<li role="presentation"><a role="menuitem" tabindex="-1"
							href="#" id="doutDriver">驾驶员</a></li>
						<li role="presentation"><a role="menuitem" tabindex="-1"
							href="#" id="doutDestination">目的地 </a></li>
						<li role="presentation"><a role="menuitem" tabindex="-1"
							href="#" id="doutCreator">创建人员 </a></li>
					</ul>
				</div>
			</form>

			<form method="post">
				<button type="button" class="btn btn-danger"
					style="float: right; margin-left: 10px;" id="deleteRecords">
					<i class="glyphicon glyphicon-trash"></i> 删除
				</button>
				<button type="button" class="btn btn-primary"
					style="float: right; margin-left: 10px;" id="addBackRecord">
					<i class="glyphicon glyphicon-log-in"></i> 回车信息登记
				</button>
				<button type="button" class="btn btn-primary" 
					style="float: right; margin-left: 10px;" id="addOutRecord">
					<i class="glyphicon glyphicon-log-out"></i> 出车信息登记
				</button>
				<hr style="clear: both;">
				<!-- 响应式表格 -->
				<div class="table-responsive">
					<table class=".table table-striped table-bordered table-hover"
						style="margin: 20px auto; width: 100%">
						<thead>
							<tr>
								<th>
									<div class="checkbox">
										<label> <input type="checkbox" id="selectAll">全选
										</label>
									</div>
								</th>
								<th>车牌号码</th>
								<th>部门</th>
								<th>出车时间</th>
								<th>预计回车时间</th>
								<th>回车时间</th>
								<th>随行人员</th>
								<th>驾驶员</th>
								<th>目的地</th>
								<th>本次行程</th>
								<th>创建人员</th>
								<th width="130px">操作</th>
							</tr>
						</thead>
						<tbody id="driveout_table">
						</tbody>
						
						<tfoot>
							<tr>
								<td colspan="12" align="center">
									<ul class="pagination">
										
									</ul>
								</td>
							</tr>
						</tfoot>
					</table>
					<div id="ps"></div>
					<input type="text" id="cur" value="" hidden="true">
					<input type="text" id="max" value="" hidden="true">
				</div>
			</form>
		</div>
	</div>

	<!-- 出车信息登记begin -->
	<div class="modal fade" tabindex="-1" role="dialog" id="doOutModal"
		aria-labelledby="gridSystemModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="gridSystemModalLabel">
						<span class="glyphicon glyphicon-plus-sign" id="cicon"></span> <span id="title">出车信息登记</span>
					</h4>
				</div>
				<div class="modal-body">

					<form class="form-horizontal" id="do_out_form">
					<input type="text" name="_method" value="" hidden="true">
					<input type="text" name="doutId" value="" hidden="true" id="outid">
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">车牌号</label>
							<div class="col-sm-8">
								<select id="carNum" class="form-control" name="doutCarlicense">
								</select>
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">部门</label>
							<div class="col-sm-8">
								<select id="depts" class="form-control" name="doutDept" >
								</select>
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">出车时间</label>
							<div class="col-sm-8">
								<input class="form-control" type="datetime-local" id="outtime"
							name="doutDeparttime" placeholder="请选择出车时间">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">预计回车时间</label>
							<div class="col-sm-8">
								<input class="form-control" type="datetime-local" id="intime"
								value="2017-06-30T00:00"    name="doutPrebacktime" placeholder="请选择预计回车时间">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">随行人员</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutFollower" id="follower"
									placeholder="请输入随行人员">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">驾驶员</label>
							<div class="col-sm-8">
								<select id="drivers" class="form-control" name="doutDriver">
								</select>
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">目的地</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutDestination" id="address"
									placeholder="请输入目的地">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">出车原因</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutReason" id="reason"
									placeholder="请输入出车原因">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">开始里程</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutStartmileage" id="startmile"
									placeholder="请输入开始里程">
							</div>
						</div>
							<input type="text"  name="doutCreator" hidden="true"
							value="${sessionScope.isAdmin==1?'超级管理员':'普通用户'}" >
					</form>

				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary"  id="do_out">保存</button>
					 
				</div>
			</div>
		</div>
	</div>
	<!-- 出车信息登记end -->

	<!-- 回车信息登记begin -->
	<div class="modal fade" tabindex="-1" role="dialog" id="addBackModal"
		aria-labelledby="gridSystemModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="gridSystemModalLabel">
						<span class="glyphicon glyphicon-ok-sign"></span> 回车信息登记
					</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" id="do_back_form">
					<input name="_method" value="put" hidden="true">
					<input name="doutId" value="" hidden="true" id="doutid">
					<input type="text" name="doutId" id="dout_id" hidden="true">
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputLarge">车牌号</label>
							<div class="col-sm-8">
								<select name="doutCarlicence" class="form-control" id="carlicense">
								</select>
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">驾驶员</label>
							<div class="col-sm-8">
								<select name="doutDriver" class="form-control" id="driver">
								</select>
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">出车时间</label>
							<div class="col-sm-8">
								<input class="form-control" type="datetime-local" 
								id="leavetime"	name="doutDeparttime" placeholder="请选择出车时间">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">回车时间</label>
							<div class="col-sm-8">
								<input class="form-control" type="datetime-local"  id="backtime" 
									name="doutBacktime"  placeholder="请选择回车时间" >
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">开始里程(公里)</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutStartmileage"
							  id="startmileage"	placeholder="请输入开始里程">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">回车里程(公里)</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutBackmileage" 
								id="backmileage"	placeholder="请输入回车里程">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">停放位置</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutParksite"  id="parksite"
									placeholder="请输入停放位置">
							</div>
						</div>
						<div class="form-group form-group-md">
							<label class="col-sm-3 control-label" for="formGroupInputSmall">备注</label>
							<div class="col-sm-8">
								<input class="form-control" type="text" name="doutRemark"  id="remark"
									placeholder="请输入备注信息">
							</div>
						</div>
					</form>

				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="add_back">保存</button>
				</div>
			</div>
		</div>
	</div>
	<!-- 回车信息登记end -->

	<!--删除确认模态框begin -->
	<div class="modal fade" tabindex="-1" role="dialog" id="deleteModal">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">请确认</h4>
				</div>
				<div class="modal-body">
					<p id="delConfirm">确定要删除吗?</p>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-primary" id="delete_record">确认</button>
				</div>
			</div>
		</div>
	</div>
	<!--删除确认模态框end-->

	<!--操作结果模态框begin -->
	<div class="modal fade" tabindex="-1" role="dialog" id="update_result">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">操作结果</h4>
				</div>
				<div class="modal-body">
					<p id="result"></p>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">确定</button>
				</div>
			</div>
		</div>
	</div>
	<!--操作结果模态框end-->
	
    <!--查看详情模态框begin-->
    <div class="modal fade" id="detailModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><i class="glyphicon glyphicon-list"></i> 出车详情</h4>
      </div>
      <div class="modal-body">
      
 <form class="form-horizontal">
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">车牌号</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="carCode" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">部    门</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="dept" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">出车时间</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="otime" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">预计回车时间</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="prebtime" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">回车时间</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="btime" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">驾驶员</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="driv" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">随行人员</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="follow" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">目的地</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="addr" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">出车原因</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="rea" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">开始里程</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="smile" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">回车里程</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="bmile" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">本次里程</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="tmile" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">停放位置</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="psite" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">备注</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="mark" placeholder="Small input" readonly="readonly">
    </div>
  </div>
  <div class="form-group form-group-md">
    <label class="col-md-3 control-label" for="formGroupInputSmall">创建人员</label>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="cp" placeholder="Small input" readonly="readonly">
    </div>
  </div>
</form> 

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
    <!--查看详情模态框end-->


	<script
		src="${pageContext.request.contextPath}/static/jquery/jquery-2.1.1.min.js"></script>
	<script
		src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript">
	//默认时间
	var myDate = new Date(), 
	Y = myDate.getFullYear(), 
	M = myDate.getMonth() + 1, 
	D = myDate.getDate();
	H=myDate.getHours(); 
	m=myDate.getMinutes();
	//处理月是一位的情况
	if((M + '').length == 1){
		M = '0' + (M + '');
	}
	//处理日是一位的情况
	if((D + '').length == 1){
		D = '0' + (D + '')
	}
	
	if((H + '').length == 1){
		H = '0' + (H + '')
	}
	if((m + '').length == 1){
		m = '0' + (m + '')
	}
	var curDay = Y + '-' + M + '-' + D+'T'+H+':'+m;
	
		$(function() {
			loadRecord();
		})
		
		function loadRecord() {
			
			 userAjax(1);
			$("#query").click(function(){
				$("#selectAll").prop("checked",false);
				userAjax(1);
			})
		}
		function userAjax(page){
			$
			.ajax({
				url : "${pageContext.request.contextPath}/driveout/"+page,
				type : "get",
				async :true,
			     dataType : "json", 
			    data:$("#query_form").serialize(),
				success : function(datas) {
				     var str = "";
					for (var i = 0; i < datas.data.list.length; i++) {
						str = str + "<tr>";
						str = str + "<td>";
						str = str
								+ "<input type='checkbox' class='selectItem' value='"+datas.data.list[i].doutId+"'>";
						str = str + "</td>";
						str = str + "<td>"
								+ datas.data.list[i].doutCarlicense
								+ "</td>";
						str = str + "<td>" + datas.data.list[i].doutDept
								+ "</td>";
						str = str + "<td width='120px'>"
								+ datas.data.list[i].doutDeparttime
								+ "</td>";
						str = str + "<td width='120px'>"
								+ datas.data.list[i].doutPrebacktime
								+ "</td>";
						if (datas.data.list[i].doutBacktime != null) {
							str = str + "<td width='80px'>"
									+ datas.data.list[i].doutBacktime
									+ "</td>";
						} else {
							str = str + "<td width='80px'>未回车</td>";
						}

						str = str + "<td>" + datas.data.list[i].doutFollower
								+ "</td>";
						str = str + "<td>" + datas.data.list[i].doutDriver
								+ "</td>";
						str = str + "<td>"
								+ datas.data.list[i].doutDestination
								+ "</td>";
						if (datas.data.list[i].doutBacktime != null) {
							str = str + "<td>"
									+ datas.data.list[i].doutThismileage
									+ "</td>";
						} else {
							str = str + "<td>未回车</td>";
						}
						str = str + "<td>" + datas.data.list[i].doutCreator
								+ "</td>";
						str = str + "<td>";
						str = str
								+ "<a class='btn btn-danger' name='deleteRecord'>";
						str = str
								+ "<span class='glyphicon glyphicon-remove'><span>";
						str = str + "</a> ";
						if(datas.data.list[i].doutBacktime != null){
						str = str + "<a class='btn btn-default' name='updateRecord'>";
						str = str
								+ "<span class='glyphicon glyphicon-edit'><span>";
						str = str + "</a> ";
						}else{
							str = str + "<a class='btn btn-success' name='updateRecord'>";
							str = str
									+ "<span class='glyphicon glyphicon-edit'><span>";
							str = str + "</a> ";
						}
						if(datas.data.list[i].doutBacktime != null){
							 str = str + "<a class='btn btn-info' name='detail' doutId='"+datas.data.list[i].doutId+"'>";
							 str = str
								+ "<span class='glyphicon glyphicon-eye-open' ><span>";
							 str = str + "</a>";
						}else{
							str = str + "<a class='btn btn-default' name='undetail'>";
							str = str
							+ "<span class='glyphicon glyphicon-eye-close'><span>";
							str = str + "</a>";
						}  
						str = str + "</td>";
						str = str + "</tr>";
					}
					$("#driveout_table").html(str); 
					
					$("#cur").val(datas.data.pageNum);
					$("#max").val(datas.data.pages);
					
					var strPage="";
					var pageNum=datas.data.pageNum;
					var pages=datas.data.pages;
					
					strPage=strPage+"<li><a  id='first'>首页</a></li>";
					if(pageNum>1){
					 strPage=strPage+"<li ><a href='#' id='before'>上一页</a></li>";
					}
					for (var i =1; i <= pages; i++) {
						strPage=strPage+"<li id='current'><a disable name='code' >"+i+"</a></li>";
					}
					if(pageNum<pages){
					strPage=strPage+"<li><a  id='after'>下一页</a></li>";
					}
					strPage=strPage+"<li><a  id='end'>尾页</a></li>";
					$(".pagination").html(strPage);
		var strp="<p>共"+pages+"页&nbsp;&nbsp;&nbsp;当前第"+pageNum+"页&nbsp;&nbsp;&nbsp;总共"+datas.data.total+"条记录</p>";    
					$("#ps").html(strp);
					isSelectAll();
					operOutRecord();
					addBackRecord();
					deleteRecords();
					deleteRecord();
					showPage();
					queryByCondition();
					selectDetail();  
				}
			})
		}
		 function selectDetail(){
			$("[name=detail]").click(function(){
				var selectId=$(this).attr("doutId");
				$.ajax({
					url : "${pageContext.request.contextPath}/detail/"+selectId,
					type : "get",
					async : true,
					dataType : "json",
					success:function(datas){
					$("#carCode").val(datas.data.doutCarlicense);
					$("#dept").val(datas.data.doutDept);
					$("#otime").val(datas.data.doutDeparttime);
					$("#prebtime").val(datas.data.doutPrebacktime);
					$("#btime").val(datas.data.doutBacktime);
					$("#driv").val(datas.data.doutDriver);
					$("#follow").val(datas.data.doutFollower);
					$("#addr").val(datas.data.doutDestination);
					$("#rea").val(datas.data.doutReason);
					$("#smile").val(datas.data.doutStartmileage);
					$("#bmile").val(datas.data.doutBackmileage);
					$("#tmile").val(datas.data.doutThismileage);
					$("#psite").val(datas.data.doutParksite);
					$("#mark").val(datas.data.doutRemark);
					$("#cp").val(datas.data.doutCreator);
				    $("#detailModal").modal("show");
					}
				})
			})
			$("[name=undetail]").click(function(){
				$("#result").html("车辆未回车登记，暂时无法查看详情！");
				$("#update_result").modal("show");
			})
		}
		 function showPage(){
			$("[name=code]").click(function(){
					var page=$(this).text();
					/* var ids =$(this).parent("li").attr("id"); */
					/* $(this).parent().first().attr("class","active"); */
					userAjax(page)
				})
			$("#before").click(function(){
			 var page=$("#cur").val()-1;
				userAjax(page); 
			})
			$("#after").click(function(){
			 var page=$("#cur").val()-(-1);
				userAjax(page); 
			})
			$("#first").click(function(){
				userAjax(1);
			})
			$("#end").click(function(){
				userAjax($("#max").val());
			})
		} 
		function queryByCondition(){
				$("[role=menuitem]").click(function(){
					var t=$(this).text();
					$("#hold").attr("placeholder","请输入"+t+",支持模糊查询");
					
					var name=$(this).attr("id");
					$("#hold").attr("name",name);
					
					t=t+"<span class='caret'></span>";
					$("#dropdownMenu1").html(t);
				})
		}
		function isSelectAll() {
			$("#selectAll").click(function() {
				var check = $(this).prop('checked');
				if (check) {
					$(":checkbox").prop('checked', true);
				} else {
					$(":checkbox").prop('checked', false);
				}
			})
			$(".selectItem").click(function() {
				var selectAll = true;
				$(".selectItem").each(function() {
					var res = $(this).prop('checked');
					if (!res) {
						selectAll = false;
						$("#selectAll").prop('checked', false);
					}
				})
				if (selectAll) {
					$("#selectAll").prop('checked', true);
				}
			})
		}
		
		function getSelect(condition) {
			 $.ajax({
					url : "${pageContext.request.contextPath}/select",
					type : "get",
					async : true,
				   dataType : "json",
				    data:{type:condition.join(",")},
					success : function(datas) {
						 var strCar = "";
						for (var i = 0; i < datas.data.cars.length; i++) {
							strCar = strCar+"<option>";
							strCar = strCar+datas.data.cars[i]+"</option>";
						}
						
						var strDriver = "";
						for (var i = 0; i < datas.data.drivers.length; i++) {
							strDriver = strDriver+"<option>";
							strDriver = strDriver+datas.data.drivers[i]+"</option>";
						}
						
						if(condition[1]!=-1){
							
							$("#carNum").html(strCar);
							$("#drivers").html(strDriver);
							var strDept = "";
							for (var i = 0; i < datas.data.depts.length; i++) {
								strDept = strDept+ "<option>";
								strDept = strDept+ datas.data.depts[i]+"</option>";
							}
						   $("#depts").html(strDept);
						
						   if(condition[1]==0){
							   $("#cicon").attr("class","glyphicon glyphicon-plus-sign");
							   $("#title").text("出车信息登记");
							   $("#carNum").attr("disabled",false);
							   $("#drivers").attr("disabled",false);
							   $("#outid").val("");
							   $("#outtime").val(curDay);
								$("#intime").val(curDay);
								$("#follower").val("");
								$("#address").val("");
								$("#reason").val("");
								$("#startmile").val("");
								return;
						   }
						}
						 
						if(condition[1]==-1){
							
							if(datas.data.driveout.doutBacktime!=null){
								   $("#result").html("该车辆已回车，请选择未回车车辆登记！")
								   $("#update_result").modal("show");
								   return ;
							}
							
							$("#carlicense").html(strCar);
							$("#driver").html(strDriver);
							$("#doutid").val(datas.data.driveout.doutId);
							
							for (var i = 0; i < datas.data.cars.length; i++) {
								if (datas.data.cars[i]==datas.data.driveout.doutCarlicense) {
									$("#carlicense").val(datas.data.cars[i]);
									break;
								}
							}
							
							$("#carlicense").attr("disabled",true);
							for (var i = 0; i < datas.data.drivers.length; i++) {
								if (datas.data.drivers[i]==datas.data.driveout.doutDriver) {
									$("#driver").val(datas.data.drivers[i]);
									break;
								}
							}
							$("#driver").attr("disabled",true);
							
							$("#leavetime").val(datas.data.driveout.doutDeparttime);
							$("#leavetime").attr("disabled",true);
							$("#startmileage").val(datas.data.driveout.doutStartmileage);
							$("#startmileage").attr("readonly",true);
							return;
						} 
						
						    $("#cicon").attr("class","glyphicon glyphicon-pencil");
						    $("#title").text("出车信息修改");
						    $("#outid").val(datas.data.driveout.doutId);
							for (var i = 0; i < datas.data.cars.length; i++) {
								if (datas.data.cars[i]==datas.data.driveout.doutCarlicense) {
									$("#carNum").val(datas.data.cars[i]);
									break;
								}
							}
							$("#carNum").attr("disabled",true);
							
							for (var i = 0; i < datas.data.drivers.length; i++) {
								if (datas.data.drivers[i]==datas.data.driveout.doutDriver) {
									$("#drivers").val(datas.data.drivers[i]);
									break;
								}
							} 
							$("#drivers").attr("disabled",true);
							
						    for (var i = 0; i < datas.data.depts.length; i++) {
								if (datas.data.depts[i]==datas.data.driveout.doutDept) {
									$("#depts").val(datas.data.depts[i]);
									break;
								}
							} 
							
							$("#outtime").val(datas.data.driveout.doutDeparttime);
							$("#intime").val(datas.data.driveout.doutPrebacktime);
							$("#follower").val(datas.data.driveout.doutFollower);
							$("#address").val(datas.data.driveout.doutDestination);
							$("#reason").val(datas.data.driveout.doutReason);
							$("#startmile").val(datas.data.driveout.doutStartmileage);
						 
					}
				})					    
		}
		
		function operOutRecord(){
		 $("#addOutRecord").click(function() {
				var condition=[0,0];
				getSelect(condition);
				$("#doOutModal").modal("show");
				doOutRecord(condition[0]);
			})
          $("[name=updateRecord]").click(function() {
        	var c=  $(this).attr("class");
        	if(c!="btn btn-default"){ 
       	    var driveoutId=$(this).parents("tr").children("td").first().children("input").val();
            var condition=[driveoutId,driveoutId];
       	     getSelect(condition);
       	    $("#doOutModal").modal("show");
       	    doOutRecord(condition[0]);
        	}else{
        		 $("#result").html("该车辆已回车，无法修改！")
				 $("#update_result").modal("show");
        	} 
          })	
		}
		
		function doOutRecord(driveoutId){
			$("#do_out").unbind("click").click(function() {
				if (driveoutId==0) {
			    $("[name=_method]").val("");
			    $("#driveoutId").val("");
				}else{
				$("[name=_method]").val("put");
				$("#driveoutId").val(driveoutId);
				}
				$.ajax({
					url : "${pageContext.request.contextPath}/driveout",
					type : "post",
					async : true,
					dataType :"json",
					data : $("#do_out_form").serialize(),
					success : function(datas) {
						$("#doOutModal").modal("hide");
						$("#result").html(datas.message);
						$("#update_result").modal("show");
						$("#selectAll").prop("checked",false);
						loadRecord();
					}
				})
			})
		}
		function deleteRecords() {
			$("#deleteRecords").click(function() {
				var selectIds = [];
				$("input:checked").each(function() {
					var id = $(this).attr("value");
					if (id != null) {
						selectIds.push(id);
					}
				})
				var idLength = selectIds.length;
				if (idLength!= 0) {
					$("#deleteModal").modal("show");
					$("#delete_record").unbind("click").click(function() {
						 $.ajax({
							url : "${pageContext.request.contextPath}/driverout",
							type : "post",
							async : true,
							dataType : "json",
							data:{doutIds:selectIds.join(","),_method:"delete"},
							success:function(datas){
								$("#deleteModal").modal("hide");
								$("#result").html(datas.message);
								$("#update_result").modal("show");
								$("#selectAll").prop("checked",false);
							    loadRecord(); 
							}
						}) 
					})
				} else {
					$("#result").html("删除至少选中一条记录");
					$("#update_result").modal("show");
				}
			})
		}
		function deleteRecord() {
			$("[name=deleteRecord]").click(
					function() {
						var selectId = $(this).parents("tr").children("td")
								.first().children("input").val();
						$("#deleteModal").modal("show");
						$("#delete_record").unbind("click").click(function() {
							$.ajax({
								url : "${pageContext.request.contextPath}/driverout",
								type : "post",
								async : true,
								dataType : "json",
								data:{doutIds:selectId,_method:"delete"},
								success:function(datas){
									$("#deleteModal").modal("hide");
									$("#result").html(datas.message);
									$("#update_result").modal("show");
									$("#selectAll").prop("checked",false);
								loadRecord();
								}
							})
						})
					})
		}
		
		 function addBackRecord() {
			$("#addBackRecord").click(function() {
				var driveoutId = [];
				$("input:checked").each(function() {
					var id = $(this).attr("value");
					if (id != null) {
						driveoutId.push(id);
					}
				}) 
				var idLength = driveoutId.length;
				if (idLength == 1) {
					var condition=[driveoutId,-1];
	            	getSelect(condition);
	            	if($("#doutid").val()!=0 || $("#doutid").val()!="" ){
	            	$("#addBackModal").modal("show");
	            	doAddBackRecord(); 
	            	}
				} else {
					$("#result").html("回车登记请选择一条记录");
					$("#update_result").modal("show");
				}
			})
		}
		function doAddBackRecord(){
			$("#add_back").unbind("click").click(function(){
				var leavetime=$("#leavetime").val();
			    var backtime=$("#backtime").val();
				var mileage=$("#backmileage").val()-$("#startmileage").val();
				if(backtime<leavetime){
						$("#result").html("回车时间小于出车时间，请检查！");
					$("#update_result").modal("show"); 
					return;
				} 
				if(mileage<0){
						$("#result").html("回车里程小于出车里程，请检查！");
					$("#update_result").modal("show"); 
					return;
				} 
			$.ajax({
				url : "${pageContext.request.contextPath}/back",
				type : "post",
				async : true,
				dataType : "json",
				data : $("#do_back_form").serialize(),
				success : function(datas) {
					$("#addBackModal").modal("hide");
						$("#result").html(datas.message);
						$("#update_result").modal("show");
						$("#selectAll").prop("checked",false);
					loadRecord();
			}
			})
			})
		}  
	</script>
</body>
</html>